package com.yc.spring.jdbc;

import com.yc.spring.bank.bean.Record;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class RecordDao {

    @Resource
    JdbcTemplate jdbc;

    public int insert(int aid, double money){
        String sql = "insert into bank_oprecord (aid,opmoney) value (?,?)";
        return jdbc.update(sql, aid, money);
    }

    public List<Record> selectByAid(int aid){
        String sql = "select * from bank_oprecord where aid = ?";
        return jdbc.query(sql, new RowMapper<Record>() {
            @Override
            public Record mapRow(ResultSet rs, int rowNum) throws SQLException {
                Record record = new Record();
                record.setId(rs.getInt("id"));
                record.setAccountId(rs.getInt("aid"));
                record.setMoney(rs.getDouble("opmoney"));
                return record;
            }
        }, aid);
    }

}
